package com.lonely.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author 独孤小标
 * @description 组件隐藏有多种方式
 * @since 2022/6/28 10:37 下午
 */
public class 组件隐藏设置对比 extends Application {

    public static void main(String[] args) {
        launch(args);
    }


    boolean isVisible = true;
    boolean isManaged = true;
    double opacity = 1;

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        //创建4个按钮作为显示用例
        HBox hBox = new HBox();
        hBox.setPadding(new Insets(30));
        hBox.setSpacing(10.0);

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        hBox.getChildren().addAll(button1, button2, button3, button4);

        //创建3个操作按钮
        VBox vBox = new VBox();
        vBox.setLayoutY(130);
        vBox.setSpacing(20.0);

        //测试是否显示，虽然不显示了，但是位置还在
        Button visibleBtn = new Button();
        visibleBtn.setText("setVisible:" + isVisible);

        //测试是否显示，不显示，且位置移除
        Button managedBtn = new Button();
        managedBtn.setText("setManaged:" + isManaged);

        //测试是否显示，不显示，只是变透明了而已
        Button opacityBtn = new Button();
        opacityBtn.setText("setOpacity:" + opacity);

        vBox.getChildren().addAll(visibleBtn, managedBtn, opacityBtn);


        //测试 可见性
        visibleBtn.setOnAction(event -> {
            //修改可见性
            isVisible = !isVisible;
            button3.setVisible(isVisible);
            visibleBtn.setText("setVisible:" + isVisible);
            printNodeSize(hBox);
        });

        //测试 移除管理
        managedBtn.setOnAction(event -> {
            isManaged = !isManaged;
            button3.setManaged(isManaged);
            managedBtn.setText("setManaged:" + isManaged);
            printNodeSize(hBox);

            //这里虽然移除了，位置可以被其他节点占用，但是还是在组件中，数量不会变
        });

        //测试 透明度
        opacityBtn.setOnAction(event -> {
            opacity = 1 - opacity;
            button3.setOpacity(opacity);
            opacityBtn.setText("setOpacity:" + opacity);
            printNodeSize(hBox);
        });

        anchorPane.getChildren().addAll(hBox, vBox);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);

        primaryStage.show();


    }


    public static void printNodeSize(HBox hBox){
        System.out.println("组件数量："+hBox.getChildren().size());
    }
}
